﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

/// <summary>
/// Summary description for SiteStatistics
/// </summary>
public class SiteStatistics
{
    private static string strIP;
    private static string strPage;
    private static string strPageRef;
    private static string strBrowser;
    private static string strTempBrowser;
    private static string strOS;
    private static StreamWriter ErrorFile;
    

    public SiteStatistics()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public  void AddLog(string fname,string lname,string email)
    {
        strTempBrowser = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString();

        strIP = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();
        strPage = System.Web.HttpContext.Current.Request.Url.ToString();
        strOS = System.Web.HttpContext.Current.Request.Browser.Platform.ToString();


        strBrowser = FindBrowser(); // finding browser through findbrowser function


        strOS = FindOS(); // finding os through findos function

        if (System.Web.HttpContext.Current.Request.UrlReferrer != null)
        {
            strPageRef = System.Web.HttpContext.Current.Request.UrlReferrer.ToString();
        }
        else
        {
            strPageRef = "http://www.avila.edu/nursing/supplement/"; // because variable cannot accept null value, may error in null exception error
        }
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["prod"].ConnectionString))
        {
            try
            {
                conn.Open();
                SqlCommand Cmd = new SqlCommand("CUS_sp_Visitor_FromCatholic_INSERT", conn);
                Cmd.CommandType = CommandType.StoredProcedure;

                Cmd.Parameters.AddWithValue("@VIP", strIP.ToString());
                Cmd.Parameters.AddWithValue("@VPage", strPage.ToString());
                Cmd.Parameters.AddWithValue("@VRef", strPageRef.ToString());
                Cmd.Parameters.AddWithValue("@VBrowser", strBrowser.ToString());
                Cmd.Parameters.AddWithValue("@VOS", strOS.ToString());
                Cmd.Parameters.AddWithValue("@fname", fname.ToString());
                Cmd.Parameters.AddWithValue("@lname", lname.ToString());
                Cmd.Parameters.AddWithValue("@email", email.ToString());
                Cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                string errMsg;
                errMsg = e.Message.ToString();
                using (ErrorFile = File.AppendText(System.Web.HttpContext.Current.Request.MapPath("SiteStatisticsLog.txt")))
                // AppendText method check for existing .txt file othervise create the new one...
                {
                    ErrorFile.WriteLine(DateTime.Now.ToString() + " : " + errMsg);
                }
                //System.Web.HttpContext.Current.Response.Write(err.Message);
            }
            finally
            {
               conn.Close();
            }
        }


    }
    private static string FindBrowser()
    {
        string _strBrowser;
        if (strTempBrowser.Contains("Chrome"))
        {
            _strBrowser = "Chrome";


        }
        else if (strTempBrowser.Contains("Avant Browser"))
        {
            _strBrowser = "Avant Browser";


        }
        else if (strTempBrowser.Contains("Googlebot"))
        {
            _strBrowser = "Googlebot";


        }
        else if (strTempBrowser.Contains("Yahoo! Slurp"))
        {
            _strBrowser = "Yahoo! Slurp";


        }
        else if (strTempBrowser.Contains("Mediapartners-Google"))
        {
            _strBrowser = "Mediapartners-Google";


        }
        else if (strTempBrowser.Contains("msnbot"))
        {
            _strBrowser = "msnbot";


        }
        else if (strTempBrowser.Contains("SurveyBot"))
        {
            _strBrowser = "SurveyBot/2.3 (Whois Source)";


        }
        else if (strTempBrowser.Contains("Baiduspider"))
        {
            _strBrowser = "Baiduspider";


        }
        else if (strTempBrowser.Contains("FeedFetcher-Google"))
        {
            _strBrowser = "FeedFetcher-Google";


        }
        else if (strTempBrowser.Contains("ia_archiver"))
        {
            _strBrowser = "ia_archiver";


        }
        else
        {
            _strBrowser = System.Web.HttpContext.Current.Request.Browser.Browser.ToString();
            _strBrowser += " " + System.Web.HttpContext.Current.Request.Browser.Version.ToString();

        }
        return _strBrowser;

    }
    private static string FindOS()
    {
        string _stros = strOS;

        if (strOS.Contains("Win"))
        {
            if (strTempBrowser.Contains("Windows NT 6.1"))
            {
                _stros = "Windows 7";

            }

            if (strTempBrowser.Contains("Windows NT 6.0"))
            {
                if (strTempBrowser.Contains("Media Center PC 5.0"))
                {
                    _stros = "Windows Vista";
                }
                else
                {
                    _stros = "Windows 2008 Server";

                }
            }

            if (strTempBrowser.Contains("Windows NT 5.2"))
            {
                _stros = "Windows 2003 Server/ XP 64-BIT";
            }
            if (strTempBrowser.Contains("Windows NT 5.1"))
            {
                _stros = "Windows XP";
            }
            if (strTempBrowser.Contains("Windows NT 5.01"))
            {
                _stros = "Windows 2000 SP1";
            }
            if (strTempBrowser.Contains("Windows NT 5.0"))
            {
                _stros = "Windows 2000";
            }
            if (strTempBrowser.Contains("Windows NT 4.0"))
            {
                _stros = "Windows NT 4.0";
            }
            if (strTempBrowser.Contains("Windows 98"))
            {
                _stros = "Windows 98";
            }
            if (strTempBrowser.Contains("Windows 95"))
            {
                _stros = "Windows 95";
            }
            if (strTempBrowser.Contains("Windows CE"))
            {
                _stros = "Windows CE";
            }
        }
        else if (strTempBrowser.Contains("Mac OS X"))
        {
            if (strTempBrowser.Contains("iPhone"))
            {
                _stros = "iPhone-Mac OS X";
            }
            else
            {
                _stros = "Mac OS X";
            }

        }
        else if (strOS.Contains("UNIX"))
        {
            if (strTempBrowser.Contains("FreeBSD"))
            {
                _stros = "FreeBSD";
            }
            else if (strTempBrowser.Contains("Linux"))
            {
                if (strTempBrowser.Contains("Ubuntu"))
                {
                    _stros = "Ubuntu";
                }
                else if (strTempBrowser.Contains("Fedora"))
                {
                    _stros = "Fedora";
                }
                else if (strTempBrowser.Contains("CentOS"))
                {
                    _stros = "CentOS";
                }
                else if (strTempBrowser.Contains("Red Hat"))
                {
                    _stros = "Red Hat";
                }
                //add more linux versions here
                else
                {
                    _stros = "Linux";
                }
            }
            else
            {
                _stros = "UNIX";
            }
        }

        else if (strOS.Contains("Unknown"))
        {
            _stros = strTempBrowser;
        }

        //_stros += " : " + strTempBrowser;//REMOVE

        return _stros;
    }

}

	

